<template>
	<div class="widgets bg-white">
        <el-input v-model.trim="searchText" placeholder="输入搜索内容" class="search__input" @keyup.enter.native="search"></el-input>
        <el-button type="primary" @click="search">查询</el-button>
        <div class="btn-group right">
            <el-button type="info" @click="refresh">刷新</el-button>
            <el-button type="primary" @click="add">新增</el-button>
            <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>删除</el-button>
            <el-button type="danger" v-else @click="del">删除</el-button>
            <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>禁用</el-button>
            <el-button type="danger" v-else @click="disabedBtn(0, '禁用')">禁用</el-button>
            <el-button type="success" v-if="multipleSelection.length <= 0" disabled>启用</el-button>
            <el-button type="success" v-else @click="disabedBtn(1, '启用')">启用</el-button>
            <el-button-group class="btn-other">
                <el-button v-if="multipleSelection.length <= 0" disabled>
                    <img src="../../../assets/images/export.png" alt="">
                </el-button>
                <el-button title="导出" v-else @click="exportExcel">
                    <img src="../../../assets/images/export.png" alt="">
                </el-button>                 
                <el-button title="打印">
                    <img src="../../../assets/images/print.png" alt="">
                </el-button>
                <el-button title="帮助">
                    <img src="../../../assets/images/help.png" alt="">
                </el-button>
            </el-button-group>
        </div>
    </div>
</template>
<script>
export default {
	data() {
		return {
			searchText: '',
			multipleSelection: this.$store.state.departmentStore.multipleSelection,
            selectRowId: this.$store.state.departmentStore.selectRowId
		}
	},
    watch: {
        searchText(newVal) {
            if(!newVal) {
                this.$store.dispatch('departmentStore/getTreeData');
            }
        }
    },
	methods: {
		search() {
            if(this.searchText) {
                let reg = new RegExp(this.searchText, 'g'),
                    newArr = [],
                    data = this.$store.state.departmentStore.searchResult;
                if(data.length > 0) {
                    data.forEach(item => {
                        if(reg.test(item['name']) || (item['categoryName'] && reg.test(item['categoryName'])) || (item['supervisorName'] && reg.test(item['supervisorName'])) || (item['toPinName'] && reg.test(item['toPinName'])) || (item['toPinParentName'] && reg.test(item['toPinParentName'])) || (item['toPinCategoryName'] && reg.test(item['toPinCategoryName'])) || (item['toPinSupervisorName'] && reg.test(item['toPinSupervisorName']))) {
                            newArr.push(item);
                        }
                    });
                    this.$store.commit('departmentStore/updateTableData2', newArr);
                }
            }
		},
		refresh() {       //刷新
            const loading = this.$loading({
                lock: true,
                text: '数据刷新中...',
                spinner: 'el-icon-loading',
                background: '#fff'
            });
            setTimeout(() => {
                loading.close();
            }, 1000);
		},
		add() {
            this.$store.commit('departmentStore/updateRowId');
            this.$util.openLayerIframe({title: '部门资料-新增', zIndex: 13, width: '600px', height: '480px', url: this.$domain + '/#/basicData/department/form'});
		},
		del() {
            let type = {
                headers: {
                    'Content-Type': 'application/json'
                }
            }
            let arr = this.multipleSelection;
            arr.forEach(item => {
                if(item['subDepartment'].length > 0) {
                    this.$message({message: '删除失败，该节点包含子节点', duration: 1500, type: 'warning'});
                    return false;
                }
                this.$confirm('确定删除选中部门?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.post('department/deleteById', JSON.stringify(this.selectRowId), type).then(res => {
                        if(res.data.code == 20001) {
                            this.$message({message: '删除部门成功', duration: 1500, type: 'success'});
                            //更新要展开高亮的节点id
                            this.$store.commit('departmentStore/updateExpandNodeId', this.multipleSelection[0]['parentId'] == 0 ? this.systemId : this.multipleSelection[0]['parentId']);
                            setTimeout(() => {
                                this.$store.dispatch('departmentStore/getTreeData');            //刷新数据
                            }, 1000);
                        } else {
                            this.$message.error(res.data.msg || '删除部门失败');
                        }
                    });
                }).catch(() => {
                    this.$message.info('已取消删除');
                });
            });
		},
		disabedBtn(status, msg) {         //禁用、启用
            var type = {
                headers: {
                    'Content-type': 'application/json;charset=UTF-8'
                }
            }
            for(var i = 0; i < this.multipleSelection.length; i++) {
                if(this.multipleSelection[i]['locks'] == status) {
                    this.$alert(msg + '失败，其中包含已'+ msg +'选项，重新选择！', msg + '提示', {
                        confirmButtonText: '确定'
                    });
                    return false;
                }
            }
            this.$http.post('department/enable?state=' + status, JSON.stringify(this.selectRowId), type).then(res => {
                if(res.data.code == 20001) {
                    this.$message({message: msg + '成功', type: 'success', duration: 2000});
                    //更新要展开高亮的节点id
                    this.$store.commit('departmentStore/updateExpandNodeId', this.multipleSelection[0]['parentId'] == 0 ? this.systemId : this.multipleSelection[0]['parentId']);
                    setTimeout(() => {
                        this.$store.dispatch('departmentStore/getTreeData');
                    }, 1000);
                }
            });
		},
        exportExcel() {         //导出
            var idSting = this.selectRowId.join(',');           //将[1,2,3]格式转为 "1,2,3"
            this.$http.get('department/export',{params: { idList: idSting }}).then(res => {
                this.$message({message: '导出成功', type: 'success'});
                window.location.href = this.$url + 'department/export?list=' + idSting;          //导出时触发
            }).catch(err => {
                this.$message.error(err);
            });
        }
	}
}
</script>